﻿
namespace TIKSN.StringSearchingAlgorithms.Tests
{
    [Microsoft.VisualStudio.TestTools.UnitTesting.TestClass]
    public class TextTests
    {
        [Microsoft.VisualStudio.TestTools.UnitTesting.TestMethod]
        public void RankLowerBound()
        {
            var AllStrings = Helper.GenerateRandomStrings();
            var TextComparers = Helper.GetAllTextComparers();

            foreach (StringSearchingAlgorithms.ITextComparer TextComparer in TextComparers)
            {
                foreach (var GeneratedString1 in AllStrings)
                {
                    foreach (var GeneratedString2 in AllStrings)
                    {
                        int Rank12 = TextComparer.Compare(GeneratedString1, GeneratedString2);

                        Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(Rank12 >= 0, "Rank mast be greater than or equal to 0. Comparer = {0}, Rank = {1}, String1 = '{2}', String2 = '{3}'", TextComparer.GetType().Name, Rank12, GeneratedString1, GeneratedString2);
                    }
                }
            }
        }

        [Microsoft.VisualStudio.TestTools.UnitTesting.TestMethod]
        public void RankUpperBound()
        {
            var AllStrings = Helper.GenerateRandomStrings();
            var TextComparers = Helper.GetAllTextComparers();

            foreach (StringSearchingAlgorithms.ITextComparer TextComparer in TextComparers)
            {
                foreach (var GeneratedString1 in AllStrings)
                {
                    int SelfRank = TextComparer.Compare(GeneratedString1, GeneratedString1);

                    foreach (var GeneratedString2 in AllStrings)
                    {
                        int Rank12 = TextComparer.Compare(GeneratedString1, GeneratedString2);

                        Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(SelfRank >= Rank12, "Maximum comparison result must be when string is compared by itself. Comparer = {0}, SelfRank = {1}, Rank = {2}, String1 = '{3}', String2 = '{4}'", TextComparer.GetType().Name, SelfRank, Rank12, GeneratedString1, GeneratedString2);
                    }
                }
            }
        }

        [Microsoft.VisualStudio.TestTools.UnitTesting.TestMethod]
        public void RankCommutativity()
        {
            var AllStrings = Helper.GenerateRandomStrings();
            var TextComparers = Helper.GetAllTextComparers();

            foreach (StringSearchingAlgorithms.ITextComparer TextComparer in TextComparers)
            {
                foreach (var GeneratedString1 in AllStrings)
                {
                    foreach (var GeneratedString2 in AllStrings)
                    {
                        int Rank12 = TextComparer.Compare(GeneratedString1, GeneratedString2);
                        int Rank21 = TextComparer.Compare(GeneratedString2, GeneratedString1);

                        Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(Rank12 == Rank21, "Rank12 must be equal to Rank21. Comparer = {0}, Rank12 = {1}, Rank21 = {2}", TextComparer.GetType().Name, Rank12, Rank21);
                    }
                }
            }
        }

        [Microsoft.VisualStudio.TestTools.UnitTesting.TestMethod]
        public void RankCaseInsensitivity()
        {
            var AllStrings = Helper.GenerateRandomStrings();
            var TextComparers = Helper.GetAllTextComparers();

            foreach (StringSearchingAlgorithms.ITextComparer TextComparer in TextComparers)
            {
                foreach (var GeneratedString1 in AllStrings)
                {
                    int SelfRank = TextComparer.Compare(GeneratedString1, GeneratedString1);

                    int RankLL = TextComparer.Compare(GeneratedString1.ToLowerInvariant(), GeneratedString1.ToLowerInvariant());
                    int RankLU = TextComparer.Compare(GeneratedString1.ToLowerInvariant(), GeneratedString1.ToUpperInvariant());
                    int RankUL = TextComparer.Compare(GeneratedString1.ToUpperInvariant(), GeneratedString1.ToLowerInvariant());
                    int RankUU = TextComparer.Compare(GeneratedString1.ToUpperInvariant(), GeneratedString1.ToUpperInvariant());

                    Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(SelfRank == RankLL && RankLL == RankLU && RankLU == RankUL && RankUL == RankUU, "Comparison must be case-insensitive. Comparer = {0}, SelfRank = {1}, RankLL = {2}, RankLU = {3}, RankUL = {4}, RankUU = {5}", TextComparer.GetType().Name, SelfRank, RankLL, RankLU, RankUL, RankUU);
                }
            }
        }

        [Microsoft.VisualStudio.TestTools.UnitTesting.TestMethod]
        public void Compare001()
        {
            //string s = "ȉևΫƠƲŸâ ƦϋƗĬ ǈġ ȬΕ";
            string s = "Ʀϋ"; // U+0x01A6 U+0x03CB
            string sl = s.ToLowerInvariant();
            string su = s.ToUpperInvariant();

            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(string.Compare(sl, su, System.StringComparison.InvariantCultureIgnoreCase) == 0);

            //Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(s.Length == sl.Length && sl.Length == su.Length);

            //var TextComparers = Helper.GetAllTextComparers();

            //var comparer = new StringSearchingAlgorithms.LongestCommonSubsequence();

            //var M = comparer.GetMatrix(s, s);
            //var MLL = comparer.GetMatrix(sl, sl);
            //var MLU = comparer.GetMatrix(sl, su);
            //var MUL = comparer.GetMatrix(su, sl);
            //var MUU = comparer.GetMatrix(su, su);

            //for (int i1 = 0; i1 < s.Length; i1++)
            //{
            //    for (int i2 = 0; i2 < s.Length; i2++)
            //    {
            //        System.Diagnostics.Debug.Write(string.Format("[{0},{1}] ", MLL[i1, i2], MLU[i1, i2]));
            //        //if(MLL[i1,i2] != MLU[i1,i2])
            //        //{

            //        //    System.Diagnostics.Debug.WriteLine(MLL[i1, i2]);
            //        //    System.Diagnostics.Debug.WriteLine(MLU[i1, i2]);
            //        //    System.Diagnostics.Debug.WriteLine(s[i1]);
            //        //    System.Diagnostics.Debug.WriteLine(s[i2]);
            //        //    System.Diagnostics.Debug.WriteLine(sl[i1]);
            //        //    System.Diagnostics.Debug.WriteLine(su[i2]);
            //        //}
            //    }
            //    System.Diagnostics.Debug.WriteLine("");
            //}

            //int Rank = comparer.Compare(s, s);
            //int RankLL = comparer.Compare(sl, sl);
            //int RankLU = comparer.Compare(sl, su);
            //int RankUL = comparer.Compare(su, sl);
            //int RankUU = comparer.Compare(su, su);

            //Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(Rank == RankLL);
            //Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(RankLL == RankLU);
            //Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(RankLU == RankUL);
            //Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(RankUL == RankUU);
        }
    }
}
